package com.joe.updemo.utils.manager;


import android.content.Context;
import android.support.annotation.StringRes;
import android.widget.Toast;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * Created by zy on 2017/4/7.
 */

public class ToastUtil {
    private static Toast mToast;
    private static Context mContext;

    public static void init(Context context) {
        mContext = context;
        if (null == mToast) {
            mToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
        }
    }

    public static void showLong(String str) {
        show(str, Toast.LENGTH_LONG);
    }

    public static void showLong(@StringRes int resId) {
        showLong(mContext.getString(resId));
    }

    public static void showShort(String str) {
        show(str, Toast.LENGTH_SHORT);
    }

    public static void showShort(@StringRes int resId) {
        showShort(mContext.getString(resId));
    }

    private static void show(final String str, final int duration) {
        Observable.just(1).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                if (null != mToast) {
                    mToast.setDuration(duration);
                    mToast.setText(str);
                    mToast.show();
                }
            }
        });
    }

    //show in order
    public static void showLongInOrder(String str) {
        showInOrder(str, Toast.LENGTH_LONG);
    }

    public static void showLongInOrder(@StringRes int resId) {
        showLongInOrder(mContext.getString(resId));
    }

    public static void showShortInOrder(String str) {
        showInOrder(str, Toast.LENGTH_SHORT);
    }

    public static void showShortInOrder(@StringRes int resId) {
        showShortInOrder(mContext.getString(resId));
    }

    private static void showInOrder(final String str, final int duration) {
        Toast.makeText(mContext, str, duration).show();
    }

}
